package com.admin.web.controller;

import com.bayss.core.constant.ResultMsg;
import com.bayss.core.entity.found.ZaihuMessageComments;
import com.bayss.core.entity.user.UserInfo;
import com.bayss.core.enums.MessageShow;
import com.bayss.core.service.found.MessageCommentsService;
import com.bayss.core.service.found.MessageLikeService;
import com.bayss.core.service.found.MessageService;
import com.bayss.core.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 留言管理
 */
@RestController
public class MessageController extends ApiBaseController {

    @Autowired
    private MessageService messageService;

    @Autowired
    private MessageCommentsService messageCommentsService;

    @Autowired
    private MessageLikeService messageLikeService;
    @Autowired
    private UserService userService;
    /**
     *分页查询留言列表
     */
    @RequestMapping(value = "/message/get/MessageListByPage.json", method = RequestMethod.POST)
    public ResultMsg getMessageListByPage(@RequestParam(value = "titleName", defaultValue = "", required = false) String titleName,
                                          @RequestParam(value = "page", defaultValue = "1", required = true) Integer page, Integer status,
                                          @RequestParam(value = "pageSize", defaultValue = "10", required = true) Integer pageSize) {
        try {
            return ResultMsg.success(messageService.getMessageListByPage(titleName, page, pageSize,status));
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.fail(250, "提交失败");
        }
    }

    /**
     * 置顶与取消置顶留言
     *
     * @param placeTop  是否置顶 0 取消 1 置顶
     * @param messageId
     * @return
     */
    @RequestMapping(value = "/message/update/MessagePlaceTopByMessageId.json", method = RequestMethod.POST)
    public ResultMsg updateMessagePlaceTopByMessageId(@RequestParam(value = "placeTop", defaultValue = "0", required = true) Integer placeTop,
                                                      @RequestParam(value = "messageId", defaultValue = "", required = true) Integer messageId) {
        try {
            messageService.updateMessagePlaceTopByMessageId(placeTop, messageId);
            return ResultMsg.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.fail(250, "提交失败");
        }
    }

    /**
     * 留言审核
     * @param status:1-待审核；2-审核通过；3-审核拒绝
     */
    @RequestMapping(value = "/message/update/messageConfirm.json", method = RequestMethod.POST)
    public ResultMsg messageConfirm(Integer id, Integer status) {
        try {
            messageService.confirmMessage(id, status);
            return ResultMsg.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.fail(250, "提交失败");
        }
    }
    /**
     * 显示和隐藏留言
     *
     * @param messageType 留言  0--显示  1--隐藏
     * @param messageId
     * @return
     */
    @RequestMapping(value = "/message/update/MessageTypeByMessageId.json", method = RequestMethod.POST)
    public ResultMsg updateMessageTypeByMessageId(@RequestParam(value = "messageType", defaultValue = "0", required = true) Integer messageType,
                                                  @RequestParam(value = "messageId", defaultValue = "", required = true) Integer messageId) {
        try {
            return ResultMsg.success(messageService.updateMessageTypeByMessageId(messageType, messageId));
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.fail(250, "提交失败");
        }
    }

    /**
     * 获取留言详情
     *
     * @param messageId
     * @return
     */
    @RequestMapping(value = "/message/get/MessageInfoByMessageId.json", method = RequestMethod.POST)
    public ResultMsg getMessageInfoByMessageId(@RequestParam(value = "messageId", defaultValue = "", required = true) Integer messageId) {
        try {
            return ResultMsg.success(messageService.getMessageInfoByMessageId(messageId));
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.fail(250, "提交失败");
        }
    }

    /**
     * 管理员评论留言
     * @param comContent
     * @param messageId
     * @return
     */
    @RequestMapping(value = "/message/add/MessageComments.json", method = RequestMethod.POST)
    public ResultMsg addMessageComments(@RequestParam(value = "comContent", defaultValue = "", required = true) String comContent,
                                        @RequestParam(value = "messageId", defaultValue = "", required = true) Integer messageId) {
        try {
            UserInfo systemUser = userService.getSystemUser();
            ZaihuMessageComments messageCommentsEntity = messageCommentsService.addMessageComments(systemUser.getId().intValue(), comContent, messageId);
            return ResultMsg.success(messageCommentsEntity);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.fail(250, "提交失败");
        }
    }

    /**
     * 隐藏显示评论
     * @param commentsId
     * @param comType  0--显示  1--隐藏
     * @return
     */
    @RequestMapping(value = "/message/update/MessageCommentsType.json", method = RequestMethod.POST)
    public ResultMsg updateMessageCommentsType(@RequestParam(value = "commentsId", defaultValue = "", required = true) Integer commentsId,
                                              @RequestParam(value = "comType", defaultValue = "", required = true) Integer comType) {
        try {
            if (comType == null || (comType != MessageShow.show && comType != MessageShow.hide)) {
                return ResultMsg.fail(250, "参数错误");
            }
            messageCommentsService.updateMessageCommentsType(commentsId,comType);
            return ResultMsg.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.fail(250, "提交失败");
        }
    }

    /**
     * 分页查询评论列表
     * @param messageId
     * @param page
     * @return
     */
    @RequestMapping(value = "/message/get/MessageCommentsListByPage.json")
    public ResultMsg getMessageCommentsListByPage(Integer messageId,Integer status,
                                                 @RequestParam(value = "page", defaultValue = "1", required = true) Integer page,
                                                 @RequestParam(value = "pageSize", defaultValue = "10", required = true) Integer pageSize) {
        try {
            return ResultMsg.success(messageCommentsService.getMessageCommentsListByPage(messageId,status,page,pageSize));
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.fail(250, "提交失败");
        }
    }

    /**
     * 评论审核
     * @param status:1-待审核；2-审核通过；3-审核拒绝
     */
    @RequestMapping(value = "/message/update/commentsConfirm.json", method = RequestMethod.POST)
    public ResultMsg commentsConfirm(Integer id, Integer status) {
        try {
            messageService.confirmComments(id, status);
            return ResultMsg.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.fail(250, "提交失败");
        }
    }

    /**
     * 管理员点赞留言
     * @param messageId
     * @return
     */
    @RequestMapping(value = "/message/add/MessageLikeByMessageId.json", method = RequestMethod.POST)
    public ResultMsg addMessageLikeByMessageId(@RequestParam(value = "messageId", defaultValue = "", required = true) Integer messageId) {
        try {
            UserInfo systemUser = userService.getSystemUser();
            return ResultMsg.success(messageLikeService.addMessageLikeByMessageId(messageId, systemUser.getId().intValue()));
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.fail(250, "提交失败");
        }
    }

    /**
     * 发现页开关设置
     */
    @RequestMapping(value = "/message/update/onOff.json")
    public ResultMsg onOff(Integer flag) {
        try {
            messageService.updateMessageOnOff(flag);
            return ResultMsg.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.fail(250, "提交失败");
        }
    }

    /**
     * 获取发现页开关
     */
    @RequestMapping(value = "/message/get/onOff.json", method = RequestMethod.GET)
    public ResultMsg getOnOff() {
        try {
            return ResultMsg.success(messageService.getMessageOnOff());
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.fail(250, "提交失败");
        }
    }
}
